package com.jwater.launch;

import java.io.File;

import com.jwater.launch.LauncherInfo.DependDeployInfo;

/**
 * @description:启动器入口类
 * @author: jnner
 * @createDate: 2021/2/5
 * @version: 1.0
 */
public class JwaterLauncher {
    public static final String DEFAULT_REGION_NAME = "default";
    public static final String JWATER_LIB_PATH_NAME = "jwater-lib";
    public static final String JWATER_CONSOLE_PATH_NAME = "jwater-console";
    public static final String BOOT_CLASS_NAME = "com.jwater.core.Bootstrap";
    public static final String CONSOLE_CLASS_NAME = "com.jwater.console.ConsoleApp";

    /**
     * 获取jwater-lib的jar目录
     *
     * @return
     */
    public static String getJwaterLibPath() {
        return DeployPathUtils.getDeployPath(DEFAULT_REGION_NAME, JWATER_LIB_PATH_NAME, null).getPath() + "/lib";
    }

    /**
     * 获取jwater-console的jar目录
     *
     * @return
     */
    public static String getJwaterConsolePath() {
        return DeployPathUtils.getDeployPath(DEFAULT_REGION_NAME, JWATER_CONSOLE_PATH_NAME, null).getPath() + "/lib";
    }

    /**
     * 启动程序
     *
     * @param launcherInfo
     * @throws Throwable
     */
    public static void launch(LauncherInfo launcherInfo) throws Throwable {
        launcherInfo.addDeployInfo(DEFAULT_REGION_NAME, JWATER_LIB_PATH_NAME, null);
        for (DependDeployInfo deployInfo : launcherInfo.getDelpoyInfo()) {
            File deployPath = DeployPathUtils.getDeployPath(deployInfo.getRegionName(), deployInfo.getDeployName(),
                    deployInfo.getVersion());
            LoaderUtils.addJarDir(new File(deployPath, "lib"), true);
        }
        LoaderUtils.exeMain(launcherInfo.getClassName(), launcherInfo.getArgs());
    }

    /**
     * 当前进程里启动className，默认加载jwater-lib的jar包
     *
     * @param className
     * @param args
     */
    public static void launcher(String className, String[] args) {
        try {
            LauncherInfo launcherInfo = LauncherInfo.build(className);
            launcherInfo.setArgs(args);
            launch(launcherInfo);
        } catch (Throwable e) {
            throw new JwaterLauncherExeception(e.toString(), e);
        }
    }

    /**
     * 当前进程里启动className，默认加载jwater-lib、jwater-console的jar包
     *
     * @param className
     * @param args
     */
    public static void launcherConsole(String className, String[] args) {
        try {
            LauncherInfo launcherInfo = LauncherInfo.build(className);
            launcherInfo.setArgs(args);
            launcherInfo.addDeployInfo(DEFAULT_REGION_NAME, JWATER_CONSOLE_PATH_NAME, null);
            launch(launcherInfo);
        } catch (Throwable e) {
            throw new JwaterLauncherExeception(e.toString(), e);
        }
    }

    /**
     * 启动Bootstrap类，默认加载jwater-lib的jar包
     *
     * @param args
     * @throws Throwable
     */
    public static void main(String[] args) throws Throwable {
        LauncherInfo launcherInfo = LauncherInfo.build(BOOT_CLASS_NAME);
        launcherInfo.setArgs(args);
        launch(launcherInfo);
    }
}
